package com.fubowen.lepton.ui.reader.recycler

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.fubowen.lepton.bean.BookBean
import com.fubowen.lepton.databinding.FragmentReaderBookshelfItemBinding
import com.fubowen.lepton.extension.toTimeString


class BookshelfAdapter() :
    ListAdapter<Bookshelf, BookshelfAdapter.ViewHolder>(DiffCallback()) {

    private var onClickListener: OnClickListener? = null

    //region onCreateViewHolder
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            FragmentReaderBookshelfItemBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }
    //endregion

    //region setOnClickListener
    fun setOnClickListener(listener: OnClickListener) {
        onClickListener = listener
    }
    //endregion

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = getItem(position)
        val book = item.book
        if (book != null) {
            holder.name.text = book.name
            holder.progress.text = createProgress(book)
            holder.description.text = createDescription(book)
            holder.tag.text = createTag(book)
        }
    }

    private fun createProgress(book: Book): String {
        if (book.readingChapter == 0) {
            return "未读过 · 共${book.chapterCount}章"
        }
        if (book.readingChapter == book.chapterCount) {
            return "已读到最新章 · 共${book.chapterCount}章"
        }
        return "${book.readingChapter}章/${book.chapterCount}章"
    }

    private fun createDescription(book: Book): String {
        return when (book.type) {
            BookBean.TYPE_LOCAL -> if (book.readingChapter != 0) "正在阅读 · 第${book.readingChapter}章 ${book.readingChapterName}" else ""
            BookBean.TYPE_SEQUENCE -> "${book.lastUpdateTime.toTimeString()}前更新 · 第${book.lastUpdateChapter}章 ${book.lastUpdateChapterName}"
            BookBean.TYPE_FINISH -> "已完结 · 共${book.chapterCount}章"
            else -> ""
        }
    }

    private fun createTag(book: Book): String {
        return when (book.type) {
            BookBean.TYPE_LOCAL -> "本地"
            BookBean.TYPE_SEQUENCE -> "连载"
            BookBean.TYPE_FINISH -> "完结"
            else -> ""
        }
    }

    //region ViewHolder
    inner class ViewHolder(
        binding: FragmentReaderBookshelfItemBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        val name = binding.bookshelfName
        val progress = binding.bookshelfProgress
        val description = binding.bookshelfDescription
        val tag = binding.bookshelfTag

        init {
            binding.root.setOnClickListener { onClickListener?.onItemClick(binding) }
        }
    }
    //endregion

    //region DiffCallback
    class DiffCallback : DiffUtil.ItemCallback<Bookshelf>() {
        override fun areItemsTheSame(oldItem: Bookshelf, newItem: Bookshelf): Boolean {
            return oldItem.book?.absolutePath == newItem.book?.absolutePath
        }

        override fun areContentsTheSame(oldItem: Bookshelf, newItem: Bookshelf): Boolean {
            return oldItem.book == newItem.book
        }
    }
    //endregion

    //region OnClickListener
    interface OnClickListener {
        fun onItemClick(view: FragmentReaderBookshelfItemBinding)
    }
    //endregion
}

